package com.yuan.model_lease.rest;


import com.yuan.model_common.dto.CommonQueryCriteria;
import com.yuan.model_lease.domain.LeaseRecords;
import com.yuan.model_lease.service.LeaseRecordsService;
import com.yuan.model_lease.service.dto.LeaseRecordsDto;
import com.yuan.model_lease.service.dto.LeaseRecordsQueryCriteria;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import me.zhengjie.annotation.Log;
import me.zhengjie.utils.PageResult;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@Api(tags = "物业便民: 小区租赁记录管理")
@RestController
@RequestMapping("/api/leaseRecords")
@RequiredArgsConstructor
public class LeaseRecordsController {

    private final LeaseRecordsService leaseRecordsService;

    @ApiOperation("导出/lease/records")
    @GetMapping(value = "/download")
    public void exportLeaseRecords(HttpServletResponse response, CommonQueryCriteria criteria) throws IOException {
        leaseRecordsService.download(leaseRecordsService.queryAll(criteria), response);
    }

    @GetMapping("/findList")
    @Log("查询/api/lease/records")
    @ApiOperation("查询/lease/records")
    public ResponseEntity<PageResult<LeaseRecordsDto>> queryLeaseRecordsList(CommonQueryCriteria criteria, Pageable pageable) {
        PageResult<LeaseRecordsDto> paymentBillPageResult = leaseRecordsService.queryAll(criteria, pageable);
        return new ResponseEntity<>(paymentBillPageResult, HttpStatus.OK);
    }

    @GetMapping("/findListByType")
    @Log("查询/api/lease/records/type")
    @ApiOperation("查询/lease/records/type")
    public ResponseEntity<PageResult<LeaseRecordsDto>> findListByType(LeaseRecordsQueryCriteria criteria, Pageable pageable) {
        PageResult<LeaseRecordsDto> paymentBillPageResult = leaseRecordsService.queryAllByType(criteria, pageable);
        return new ResponseEntity<>(paymentBillPageResult, HttpStatus.OK);
    }


    @Log("新增/api/lease/records")
    @ApiOperation("新增/lease/records")
    @PostMapping("/create")
    public ResponseEntity<Object> createLeaseRecords(@Validated @RequestBody LeaseRecords resources) {
        leaseRecordsService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @Log("修改/api/lease/records")
    @ApiOperation("修改/api/lease/records")
    @PostMapping("/update")
    public ResponseEntity<Object> updateLeaseRecords(@Validated(LeaseRecords.Update.class) @RequestBody LeaseRecords resources) throws Exception {
        leaseRecordsService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除/api/lease/records")
    @ApiOperation("删除/api/lease/records")
    @PostMapping("/delete")
    public ResponseEntity<Object> deleteLeaseRecords(@RequestParam Integer ids) {
        leaseRecordsService.delete(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

}
